package com.gba.mark.exam.service.Impl;


//@Service
//public class ExamQuestionsImpl extends ServiceImpl<ExamQuestionsMapper, ExamQuestions> implements ExamQuestionsService {
//
//    @Resource
//    private ExamQuestionsMapper examQuestionsMapper;
//
//    @Resource
//    private ExamPaperMapper examPaperMapper;
//
//    @Autowired
//    private ExamQuestionsService examQuestionsService;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gba.mark.exam.entity.ExamAnswer;
import com.gba.mark.exam.entity.ExamQuestions;
import com.gba.mark.exam.entity.ExamResult;
import com.gba.mark.exam.mapper.ExamReusltMapper;
import com.gba.mark.exam.service.ExamQuestionsService;
import com.gba.mark.exam.service.ExamResultService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
public class ExamResultImpl extends ServiceImpl<ExamReusltMapper, ExamResult> implements ExamResultService {

    @Resource
    private ExamQuestionsService examQuestionsService;

    /**
     * 新建并保存一个考试结果
     */

    @Override
    public Integer saveResult(List<ExamQuestions> answer, List<ExamAnswer> user_answer) {

        //对Exam_Question查询出正确答案answer
        for (ExamQuestions answers : answer) {
            examQuestionsService.saveQuestion(answers);


        }
        //计算用户输入的答案与正确答案有几个相符
        int count = 0;
        for(int i=0; i<answer.size(); i++){
            for (int j =0;j<user_answer.size(); j++){
                if(answer.get(i).equals(user_answer.get(j))){
                    count = count + 1;
                }
            }
        }
        //成绩为答对的题目占整张试卷的百分比
        int score = (int) ((new BigDecimal((float)count/ answer.size())
                .setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue())*100);
        //返回ExamResult表

        ExamResult examResult = new ExamResult();

        examResult.setUser_id(examResult.getGroup_id());
        examResult.setId(examResult.getId());
        examResult.setUser_id(examResult.getUser_id());
        examResult.setScore(score);

        return this.baseMapper.insert(examResult);
    }
}
